////////////////////////////////////////////////////////////////
// frmMain.cs
// 
// Description: Binary, Decimal and Hexidecimal Converter
//
// IDE: Microsoft Visual Studio 2005 Professional
// Langueage: c sharp
//
// $Id: frmMain.cs 649 2010-01-11 20:27:08Z snixon $
///////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace BinDecHexConverter
{
    public partial class frmMain : Form
    {
        Int64 i64BufferHex = 0;
        Int64 i64BinaryLimit = 128;
        Int64 i64buffer = 0;
        Int64 i64bAfter2sC;
        Int64 i64NegativeBinaryLimit = -256;
        Int64 i64BinaryLength = 8;

        // Create an instance of NumericTextBox.
        NumericTextBox txtDec = new NumericTextBox();
        BinaryTextBox txtBin = new BinaryTextBox();
        HexadecimalTextBox txtHex = new HexadecimalTextBox();

        public frmMain()
        {
            InitializeComponent();
            
            //place the textbox on form1
            txtDec.Parent = this;
            txtBin.Parent = this;
            txtHex.Parent = this;

            //create event handler for the new textbox
            this.txtDec.KeyUp += new System.Windows.Forms.KeyEventHandler(txtDec_KeyUp);
            this.txtDec.Enter += new System.EventHandler(txtDec_Enter);

            //create event handler for the new textbox
            this.txtBin.KeyUp += new System.Windows.Forms.KeyEventHandler(txtBin_KeyUp);
            this.txtBin.Enter += new System.EventHandler(txtDec_Enter);

            //create event handler for the new textbox
            this.txtHex.KeyUp += new System.Windows.Forms.KeyEventHandler(txtHex_KeyUp);
            this.txtHex.Enter += new System.EventHandler(txtHex_Enter);
        }

        /// <summary>
        /// Convert decimal to binary
        /// </summary>
        /// <param name="Decimal">the decimal number to convert</param>
        /// <returns>Returns the binary number converted</returns>
        private string ToBinary(Int64 Decimal)
        {
            // Declare a few variables we're going to need
            Int64 BinaryHolder;
            char[] BinaryArray;
            string BinaryResult = "";

            while (Decimal > 0)
            {
                BinaryHolder = Decimal % 2;
                BinaryResult += BinaryHolder;
                Decimal /= 2;
            }

            // The algoritm gives us the binary number in reverse order (mirrored)
            // We store it in an array so that we can reverse it back to normal
            BinaryArray = BinaryResult.ToCharArray();
            Array.Reverse(BinaryArray);
            BinaryResult = new string(BinaryArray);
            
            return BinaryResult;
        }

        /// <summary>
        /// Converts from decimal to (binary and hexidecimal)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtDec_KeyUp(object sender, KeyEventArgs e)
        {
            
            // check if textbox is empty
            if (txtDec.Text == "")
            {
                txtBin.Text = "";
                txtHex.Text = "";
                txt2s_compliment.Text = "";
            }
            else
            {
                try
                {
                    if (Int64.Parse(txtDec.Text) < (i64BinaryLimit * 2))
                    {
          
                        // convert to binary
                        txtBin.Text = ToBinary(Convert.ToInt64(txtDec.Text));

                        // 2's compliment convertion
                        i64buffer = Int64.Parse(txtDec.Text);
                        if (i64buffer <= (i64BinaryLimit - 1))
                        {
                            txt2s_compliment.Text = txtDec.Text;
                        }
                        else
                        {
                            i64bAfter2sC = ~i64buffer + 1;
                            i64bAfter2sC -= i64NegativeBinaryLimit;
                            i64bAfter2sC *= -1;
                            txt2s_compliment.Text = i64bAfter2sC.ToString();
                        }


                        // convert to hex
                        i64BufferHex = Int64.Parse(txtDec.Text);
                        txtHex.Text = i64BufferHex.ToString("X");
                    }
                    else
                    {
                        txtBin.Text = "";
                        txtHex.Text = "";
                        txt2s_compliment.Text = "";
                        MessageBox.Show("Input greater than binary limit");
                    }

                }
                catch (Exception)
                {
                    txtBin.Text = "";
                    txtHex.Text = "";
                    txt2s_compliment.Text = "";
                    MessageBox.Show("\n\nPlease enter 0 to 9 only\n\nNumbers must not be negative", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            if ((e.KeyValue < '0' || e.KeyValue > '9')) e.Handled = true;
        }

        /// <summary>
        /// Converts from binary to (decimal and hexidecimal)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBin_KeyUp(object sender, KeyEventArgs e)
        {
            Int64 buffer = 0;
            char[] szArrayLength;

            // check to see if the textbox is empty
            if (txtBin.Text == "")
            {
                txtDec.Text = "";
                txtHex.Text = "";
                txt2s_compliment.Text = "";
            }
            else
            {
                try
                {
                    szArrayLength = txtBin.Text.ToCharArray();
                    if(szArrayLength.Length <= i64BinaryLength)
                    {
                        // convert to decimal
                        txtDec.Text = Convert.ToInt64(Convert.ToInt64(txtBin.Text, 2)).ToString();

                        // 2's compliment convertion
                        i64buffer = Int64.Parse(txtDec.Text);
                        if (i64buffer <= (i64BinaryLimit - 1))
                        {
                            txt2s_compliment.Text = txtDec.Text;
                        }
                        else
                        {
                            i64bAfter2sC = ~i64buffer + 1;
                            i64bAfter2sC -= i64NegativeBinaryLimit;
                            i64bAfter2sC *= -1;
                            txt2s_compliment.Text = i64bAfter2sC.ToString();
                        }

                        // convert to hex
                        buffer = Int64.Parse(txtDec.Text);
                        txtHex.Text = buffer.ToString("X");
                    }
                    else
                    {
                        txtDec.Text = "";
                        txtHex.Text = "";
                        txt2s_compliment.Text = "";
                        MessageBox.Show("Input greater than binary limit");
                    }
                }
                catch (Exception)
                {
                    txtDec.Text = "";
                    txtHex.Text = "";
                    txt2s_compliment.Text = "";
                    MessageBox.Show("Please enter 0's or 1's only", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// Converts from hexidecimal to (binary and decimal)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtHex_KeyUp(object sender, KeyEventArgs e)
        {
            // Check to see if textbox is empty
            if (txtHex.Text == "")
            {
                txtDec.Text = "";
                txtBin.Text = "";
                txt2s_compliment.Text = "";
            }
            else
            {
                try
                {
                    // convert to decimal
                    Int64 i64decAgain = Int64.Parse(txtHex.Text, System.Globalization.NumberStyles.HexNumber);

                    if (i64decAgain < (i64BinaryLimit * 2))
                    {
                        txtDec.Text = i64decAgain.ToString();

                        // 2's compliment convertion
                        i64buffer = Int64.Parse(txtDec.Text);
                        if (i64buffer <= (i64BinaryLimit - 1))
                        {
                            txt2s_compliment.Text = txtDec.Text;
                        }
                        else
                        {
                            i64bAfter2sC = ~i64buffer + 1;
                            i64bAfter2sC -= i64NegativeBinaryLimit;
                            i64bAfter2sC *= -1;
                            txt2s_compliment.Text = i64bAfter2sC.ToString();
                        }

                        // convert to binary
                        txtBin.Text = ToBinary(Convert.ToInt64(txtDec.Text));
                    }
                    else
                    {
                        txtDec.Text = "";
                        txtBin.Text = "";
                        txt2s_compliment.Text = "";
                        MessageBox.Show("Input greater than binary limit");
                    }
                }
                catch (Exception)
                {
                    txtDec.Text = "";
                    txtBin.Text = "";
                    txt2s_compliment.Text = "";
                    MessageBox.Show("Please enter 0 to 9 & A to F only", "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        /// <summary>
        /// Clears all textbox's
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtDec_Enter(object sender, EventArgs e)
        {
            txtDec.Text = "";
            txtBin.Text = "";
            txtHex.Text = "";
            txt2s_compliment.Text = "";
        }
        
        /// <summary>
        /// Clears all textbox's
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtBinTextbox_Enter(object sender, EventArgs e)
        {
            txtDec.Text = "";
            txtBin.Text = "";
            txtHex.Text = "";
            txt2s_compliment.Text = "";
        }

        /// <summary>
        /// Clears all textbox's
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtHex_Enter(object sender, EventArgs e)
        {
            txtDec.Text = "";
            txtBin.Text = "";
            txtHex.Text = "";
            txt2s_compliment.Text = "";
        }

        /// <summary>
        /// Exit menu item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }    

        /// <summary>
        /// show the about form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 About = new AboutBox1();
            About.Show();
        }

        /// <summary>
        /// Open help menu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void f1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Process.Start("C:\\Program Files\\DBH2C Converter\\help.txt");
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Text = Application.ProductName;

            //Draw the bounds of the NumericTextBox.
            txtDec.Bounds = new Rectangle(96, 48, 269, 20);
            txtDec.TextAlign = HorizontalAlignment.Right;
            txtDec.TabIndex = 0;
            txtDec.TabStop = true;
            txtDec.MaxLength = 3;

            //Draw the bounds of the BinaryTextBox.
            txtBin.Bounds = new Rectangle(96, 100, 269, 20);
            txtBin.TextAlign = HorizontalAlignment.Right;
            txtBin.TabIndex = 1;
            txtBin.TabStop = true;
            txtBin.MaxLength = 8;

            //Draw the bounds of the BinaryTextBox.
            txtHex.Bounds = new Rectangle(96, 126, 269, 20);
            txtHex.TextAlign = HorizontalAlignment.Right;
            txtHex.TabIndex = 2;
            txtHex.TabStop = true;
            txtHex.MaxLength = 2;
        }

        /// <summary>
        /// Set 8 bit binary limit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sCompliment_8bitBinaryLimitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            i64BinaryLength = 8;
            i64BinaryLimit = 128;
            i64NegativeBinaryLimit = -256;

            txtDec.Text = "";
            txtBin.Text = "";
            txtHex.Text = "";
            txt2s_compliment.Text = "";

            txtDec.MaxLength = 3;
            txtBin.MaxLength = 8;
            txtHex.MaxLength = 2;
            

            if (i64BinaryLimit == 128)
            {
                toolStripStatusLabel1.Text = "Binary Limit: 8 bit";
                sCompliment_8bitBinaryLimitToolStripMenuItem.Checked = true;
                sCompliment_16bitBinaryLimitToolStripMenuItem.Checked = false;
                sCompliment_32bitBinaryLimitToolStripMenuItem.Checked = false;
            }
        }

        /// <summary>
        /// Set 16 bit binary limit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sCompliment_16bitBinaryLimitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            i64BinaryLength = 16;
            i64BinaryLimit = 32768;
            i64NegativeBinaryLimit = -65536;

            txtDec.Text = "";
            txtBin.Text = "";
            txtHex.Text = "";
            txt2s_compliment.Text = "";

            txtDec.MaxLength = 5;
            txtBin.MaxLength = 16;
            txtHex.MaxLength = 4;

            if (i64BinaryLimit == 32768)
            {
                toolStripStatusLabel1.Text = "Binary Limit: 16 bit";
                sCompliment_8bitBinaryLimitToolStripMenuItem.Checked = false;
                sCompliment_16bitBinaryLimitToolStripMenuItem.Checked = true;
                sCompliment_32bitBinaryLimitToolStripMenuItem.Checked = false;
            }
        }

        /// <summary>
        /// Set 32 bit binary limit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sCompliment_32bitBinaryLimitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            i64BinaryLength = 32;
            i64BinaryLimit = 2147483648;
            i64NegativeBinaryLimit = -4294967296;

            txtDec.Text = "";
            txtBin.Text = "";
            txtHex.Text = "";
            txt2s_compliment.Text = "";

            txtDec.MaxLength = 10;
            txtBin.MaxLength = 32;
            txtHex.MaxLength = 8;

            if (i64BinaryLimit == 2147483648)
            {
                toolStripStatusLabel1.Text = "Binary Limit: 32 bit";
                sCompliment_8bitBinaryLimitToolStripMenuItem.Checked = false;
                sCompliment_16bitBinaryLimitToolStripMenuItem.Checked = false;
                sCompliment_32bitBinaryLimitToolStripMenuItem.Checked = true;
            }
        }
    }
}